package com.example.zsf.view;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.zsf.R;
import com.example.zsf.base.Presenter;
import com.example.zsf.base.Result;
import com.example.zsf.base.ZSFBaseActivity;
import com.example.zsf.model.CollectRecordModel;
import com.example.zsf.model.MobileDetailModel;
import com.example.zsf.presenter.MobilePresenter;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EnterInfomationDialog extends Dialog {


    @BindView(R.id.textView62)
    TextView server_tv;
    @BindView(R.id.textView63)
    TextView price_tv;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.editTextTextPersonName)
    EditText account_etv;
    @BindView(R.id.editTextTextPersonName2)
    EditText confim_account_etv;
    @BindView(R.id.rechargeTv)
    TextView rechargeTv;
    @BindView(R.id.rechargeEt)
    EditText rechargeEt;

    private ZSFBaseActivity context;
    private MobileDetailModel mobileModel;
    private CollectRecordModel.CollectRecordListModel collectRecordListModel;
    private EnterInfoListener listener;
    private List<String> serverList;

    /**
     * 两个数据来源 mobileModel  collectRecordListModel  所以只需要传一个就够了
     *
     * @param context
     * @param mobileModel
     * @param collectRecordListModel
     */
    public EnterInfomationDialog(@NonNull ZSFBaseActivity context, MobileDetailModel mobileModel, CollectRecordModel.CollectRecordListModel collectRecordListModel, EnterInfoListener listener) {
        super(context, R.style.CustomDialog);
        this.context = context;
        this.mobileModel = mobileModel;
        this.collectRecordListModel = collectRecordListModel;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter_info_dialog);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        if (mobileModel != null) {
            price_tv.setText(mobileModel.getGift_price());
            server_tv.setText(mobileModel.getAds_ip());
            requestPayList(mobileModel.getAds_id());
        } else {
            price_tv.setText(collectRecordListModel.getRcv_price());
            server_tv.setText(collectRecordListModel.getRcv_deal_remark());
            requestPayList(collectRecordListModel.getAds_id());
        }

        String serviceID = SPUtils.getInstance().getString("serviceID");
        if (!StringUtils.isEmpty(serviceID)) {
            account_etv.setText(serviceID);
        }
    }

    @OnClick({R.id.button4, R.id.button5, R.id.imageView8})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button4: {
                //确定
                String openName = StringUtils.isEmpty(rechargeTv.getText().toString()) ? rechargeEt.getText().toString() : rechargeTv.getText().toString();
                if (StringUtils.isEmpty(openName)) {
                    ToastUtils.showShort("请选择服务器");
                }
                if (StringUtils.isEmpty(account_etv.getText().toString())) {
                    ToastUtils.showShort("请输入充值账号");
                }

                if (!account_etv.getText().toString().equals(confim_account_etv.getText().toString())) {
                    ToastUtils.showShort("两次输入的账号不一致");
                }

                requestEnterInfo();
            }
            break;
            case R.id.button5:
            case R.id.imageView8: {
                //取消
                cancel();
            }
            break;
        }
    }

    private void requestPayList(String id) {
        context.disposed(MobilePresenter.getPayList(id, new Presenter.PresenterListener<Result<List<String>>>() {
            @Override
            public void onSuccess(Result<List<String>> object) {
                if (object.code.equals("1")) {
                    serverList = object.data;
                    if (serverList.size() == 0) {
                        rechargeEt.setVisibility(View.VISIBLE);
                        rechargeTv.setVisibility(View.GONE);
                    } else {
                        rechargeEt.setVisibility(View.GONE);
                        rechargeTv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                SelectedListDialog listDialog = new SelectedListDialog(context, serverList, new SelectedListDialog.SelectedListener() {
                                    @Override
                                    public void selected(String s) {
                                        rechargeTv.setText(s);
                                    }
                                });
                                listDialog.show();
                            }
                        });
                    }
                } else {
                    ToastUtils.showShort(object.msg);
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        }));
    }

    private void requestEnterInfo() {
        String id = mobileModel == null ? collectRecordListModel.getAds_id() : mobileModel.getAds_id();
        String openName = StringUtils.isEmpty(rechargeTv.getText().toString()) ? rechargeEt.getText().toString() : rechargeTv.getText().toString();
        context.disposed(MobilePresenter.enterInfo(id, account_etv.getText().toString(), confim_account_etv.getText().toString(), openName, new Presenter.PresenterListener<Result<HashMap>>() {
            @Override
            public void onSuccess(Result<HashMap> object) {
                if (object.code.equals("1")) {
                    // 保存常用
                    if (checkBox.isChecked()) {
                        SPUtils.getInstance().put("serviceID", account_etv.getText().toString());
                    }

                    if (listener != null) {
                        listener.confirmCallBack();
                    }

                    dismiss();
                } else {
                    ToastUtils.showShort(object.msg);
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        }));
    }


    public interface EnterInfoListener {
        void confirmCallBack();
    }

}
